package jjn.carl.binary_tree;

import commons.BinaryTreeHelper;
import commons.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jiang Jining
 * @since 2023-07-16 10:10
 */
public class LeetCode513 {
    
    private int maxDepth = -1;
    private int bottomLeftValue;
    
    public int findBottomLeftValue(TreeNode root) {
        traversal(root, 0);
        return bottomLeftValue;
    }
    
    private void traversal(TreeNode node, int depth) {
        if (node.left == null && node.right == null) {
            if (depth > maxDepth) {
                maxDepth = depth;
                bottomLeftValue = node.val;
            }
            return;
        }
        if (node.left != null) {
            traversal(node.left, depth + 1);
        }
        if (node.right != null) {
            traversal(node.right, depth + 1);
        }
    }
    
    public static void main(String[] args) {
        List<Integer> input = new ArrayList<>(List.of(1));
        TreeNode node = BinaryTreeHelper.buildTree(input);
        int bottomLeftValue = new LeetCode513().findBottomLeftValue(node);
        System.out.println("bottomLeftValue = " + bottomLeftValue);
    }
}
